# -*- coding: UTF-8 -*-

import numpy as np

from sklearn import linear_model, datasets, metrics
from sklearn.cross_validation import train_test_split
from sklearn.neural_network import BernoulliRBM
from sklearn.pipeline import Pipeline

digits = datasets.load_digits()
x = np.asarray(digits.data, 'float32')
y = np.asarray(digits.target, 'float32')
x = (x - np.min(x, 0)) / (np.max(x, 0) + 0.0001)
X_train, X_test, Y_train, Y_test = train_test_split(x, y, test_size=0.2, random_state=0)

#create model
logistic = linear_model.LogisticRegression()
rbm = BernoulliRBM(random_state=0, verbose=True)
classifier = Pipeline(steps=[('rbm', rbm), ('logistic', logistic)])

#train the model
rbm.learning_rate = 0.06
rbm.n_iter = 20
rbm.n_components = 100
logistic.C = 6000.0

#train the model base on RBM-Logistic Pipeline
classifier.fit(X_train, Y_train)

#train the model base on origin data
logistic_classifier = linear_model.LogisticRegression(C=100.0)
logistic_classifier.fit(X_train, Y_train)

#result evaluation
print("Logistic regression using RBM features:\n%s\n" % (
    metrics.classification_report(
        Y_test,
        classifier.predict(X_test))))
print("Logistic regression using raw pixel features:\n%s\n" % (
    metrics.classification_report(
        Y_test,
        logistic_classifier.predict(X_test))))

print 'job done'
